///* 
// * File:   main.cpp
// * Author: Davit Vardanyan
// *
// * Created on December 16, 2012, 6:19 PM
// */
//
//#include <cstdlib>
//#include <GL/gl.h>
//#include <GL/glut.h>
//#include <math.h>
//#include <stdlib.h>
//#include <stdio.h>
//#include <cassert>
//#include <string>
//#include <sstream>
//#include <iostream>
//#include <time.h>
//
//#include "BGStone.h"
//#include "BGPoint.h"
//#include "BGTexture.h"
//
//using namespace std;
//
//#define STONES_COUNT 1
//
//void init();
//void display();
//void enable (void);
//int windowWidth = 915;
//int windowHeight = 769;
//BGStone* myStones;
//BGTexture textures;
//int currentStoneIndex = -1;

//void enable (void) {
//        glEnable(GL_NORMALIZE);
//        glEnable(GL_COLOR_MATERIAL);
//        glEnable(GL_TEXTURE_2D);
////        glEnable(GL_BLEND);
////        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//}
//
//void init()
//{
//////        textures.initTextures();
////        myStones = new BGStone[STONES_COUNT];
////        for (int i = 0; i < STONES_COUNT; ++i) {
////            	BGPoint pos;
////                pos.x = 50 * i + 200;
////                pos.y = 50 * i + 200;
////                myStones[i].createStone(pos, i, 25, true);
////        }
//////        GLuint texId = textures.getTexture(STONE_BLACK);
//}

void
Display(void)
{
	glClearColor (0.0,0.0,0.0,1.0);
        glClear (GL_COLOR_BUFFER_BIT); 
        enable();
        for (int i = 0; i < STONES_COUNT; i++) {
                myStones[i].draw();
        }
        glFinish();
}

void
Reshape(GLint w, GLint h)
{
  windowWidth = w;
  windowHeight = h;

  glViewport(0, 0, w, h);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, w, 0, h, -1.0, 1.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

float getDistance(BGPoint from, BGPoint to)
{
    return sqrt(pow((from.x - to.x) ,2) + pow((from.y - to.y), 2));
}

void touchBegan(int button,int state, int x, int y)
{
    BGPoint touch;
    touch.x = (float)x;
    touch.y = windowHeight - (float)y;
    float distance;
    if (state == GLUT_DOWN) {
        for (int i = 0; i < STONES_COUNT; ++i)
        {
            distance = getDistance(touch ,myStones[i].getPosition());
            if (distance < myStones[i].getRadius()){
                currentStoneIndex = i;
                break;
            }
        }
    } else if (state == GLUT_UP) {
        currentStoneIndex = -1;
    }
}
void touchMoved(int x, int y)
{
    BGPoint touch;
    touch.x = x;
    touch.y = windowHeight - y;
    if (currentStoneIndex != -1) {
        myStones[currentStoneIndex].setPosition(touch);
        printf("stoneX = %f, stoneY = %f, \n", myStones[currentStoneIndex].getPosition().x,
        myStones[currentStoneIndex].getPosition().y);
//        myStones[currentStoneIndex].draw();
//        glMatrixMode(GL_MODELVIEW);
//        glLoadIdentity();
    }
}

void idle()
{
        glutPostRedisplay();
}

main(int argc, char *argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA);   
  glutInitWindowSize(windowWidth, windowHeight);
  glutCreateWindow("Backgammon");
  init();  
  glutMouseFunc(touchBegan);
  glutMotionFunc(touchMoved);
  glutDisplayFunc(Display);
  glutReshapeFunc(Reshape);
//  glutIdleFunc(idle); 
  glutMainLoop();
}